class Machine < ActiveRecord::Base
  require 'ping'
  
  belongs_to :salles  
  has_many :incidents
  validates_presence_of :nom_machine, :message => "Le nom est obligatoire"
  validates_presence_of :adresse_ip, :message => "L'adresse ip est obligatoire"
  validates_format_of :adresse_ip, :with => /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/, :on => :create, :message => "L'adresse ip n'est pas au bon format"
  
    def self.PingAllMachines
      @machines = Machine.all(:conditions => "salle_id > 0")
        @machines.each do |machine|
          puts "Ping machine "+machine.nom_machine.to_s
          if Ping.pingecho(machine.adresse_ip, 5, "echo") 
            machine.update_attributes(:ping => '1')
            Incident.gererIncidentPing(machine.id, true)          
          else
            machine.update_attributes(:ping => '0')
            Incident.gererIncidentPing(machine.id, false) 
          end
        end
    end 
    
    
  
end
